package com.jeecg.util.sms;

import com.jeecg.util.AssertUtil;
import com.jeecg.util.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;


/**
 * 短信接口
 * Created by xiaoyu on 2017-05-25.
 */
public class SmsUtil {

    private static Logger logger = LoggerFactory.getLogger(SmsUtil.class);

    private static Integer APPID  =  null;
    private static String APPKEY = null;
    private static Integer TMPID = null;

    static {
        APPID = Integer.parseInt(AssertUtil.getPropValue("sms.APPID"));
        APPKEY = AssertUtil.getPropValue("sms.APPKEY");
        TMPID = Integer.parseInt(AssertUtil.getPropValue("sms.TMPID"));
    }

    /**
     * 发送短信
     * @param tel 手机号码
     * @return
     */
    public static SmsSingleSenderResult sendSms(String tel) {
        //初始化单发
        SmsSingleSender singleSender = null;
        SmsSingleSenderResult singleSenderResult = null;
        try {
            singleSender = new SmsSingleSender(APPID, APPKEY);

            ArrayList<String> params = new ArrayList<>();
            String code = CommonUtil.getFourRandom();
            params.add(code);
            singleSenderResult = singleSender.sendWithParam("86", tel, TMPID, params, "", "", "");
            singleSenderResult.setVerificationCode(code);
            logger.info("send sms return result : " + singleSender);
        } catch (Exception e) {
            logger.info("send sms error:" + e.getMessage());
        }
        return singleSenderResult;
    }

}
